﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Windows.Forms;
using Util.Database;


namespace ConfigureNZBLoader
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

/*        private void openDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            object obj = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\AutoNZBLoader", "InstallPath", "");
            if (obj != null)
            {
                string path = obj.ToString();
                if (!path.EndsWith("\\"))
                {
                    path += "\\";
                }
                path += "database.db3";
                if (System.IO.File.Exists(path))
                {
                    openDB(path);
                    return;
                }
            }

            openDBToolStripMenuItem1_Click(sender, e);
        }*/

        private void openDBToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == ofd.ShowDialog())
            {
                openDB(ofd.FileName);
            }
        }

        class ShowListAdapter
        {
            public string showname;
            public string namequery;
            public string detailsquery;
            public uint id;

            public ShowListAdapter(uint id, string showname, string namequery, string detailsquery)
            {
                this.showname = showname;
                this.id = id;
                this.detailsquery = detailsquery;
                this.namequery = namequery;
            }

            public override string ToString()
            {
                return showname;
            }
        }

        private void openDB(string filename)
        {
            if (DB.IsOpen())
            {
                DB.Close();
            }

            DB.Open("System.Data.SQLite", "Data Source=\"" + filename + "\"");

            toolStripFilename.Text = filename;

            DB.Initialize();
            DB.Cleanup();

            tasksToolStripMenu.Visible = true;
            lstShow.Visible = true;

            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from show";
                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        lstShow.Items.Add(new ShowListAdapter((uint)(long)dr["id"], dr["showname"].ToString(), dr["namequery"].ToString(), dr["detailsquery"].ToString()));
                    }
                }
            }
        }


        private void newDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == svd.ShowDialog())
            {
                System.IO.File.Delete(svd.FileName);

                openDB(svd.FileName);
            }
        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (DB.IsOpen())
            {
                DB.Close();
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            lstShow.Visible = false;
        }


        private void lstShow_DoubleClick(object sender, EventArgs e)
        {
            editDownloadRequests(sender, e);
        }


        private void editShow(object sender, EventArgs e)
        {
            ShowListAdapter sl = (ShowListAdapter)lstShow.SelectedItem;
            if (sl != null)
            {
                EditShow dlg = new EditShow();
                dlg.DetailsQuery = sl.detailsquery;
                dlg.NameQuery = sl.namequery;
                dlg.ShowName = sl.showname;
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    lstShow.Items[lstShow.SelectedIndex] = new ShowListAdapter(sl.id, dlg.ShowName, dlg.NameQuery, dlg.DetailsQuery);

                    using (IDbCommand cmd = DB.CreateCommand())
                    {
                        cmd.CommandText = "update show set showname=?, namequery=?, detailsquery=? where id=?";
                        IDataParameter par = cmd.CreateParameter();
                        par.Value = dlg.ShowName;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = dlg.NameQuery;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = dlg.DetailsQuery;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = sl.id;
                        cmd.Parameters.Add(par);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }


        private void deleteShow(object sender, EventArgs e)
        {
            ShowListAdapter sl = (ShowListAdapter)lstShow.SelectedItem;
            if (sl != null)
            {
                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "delete from show where id=?";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = sl.id;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "delete from downloaded where id_show=?";
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "delete from request where id_show=?";
                    cmd.ExecuteNonQuery();
                }
                lstShow.Items.Remove(lstShow.SelectedItem);
                lstShow.SelectedItem = null;
            }
        }


        private void createShow(object sender, EventArgs e)
        {
            EditShow dlg = new EditShow();
            dlg.DetailsQuery = "";
            dlg.NameQuery = "";
            dlg.ShowName = "";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "insert into show(showname, namequery, detailsquery) values (?, ?, ?)";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = dlg.ShowName;
                    cmd.Parameters.Add(par);
                    par = cmd.CreateParameter();
                    par.Value = dlg.NameQuery;
                    cmd.Parameters.Add(par);
                    par = cmd.CreateParameter();
                    par.Value = dlg.DetailsQuery;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                    cmd.CommandText = "select max(id) from show";
                    uint id = (uint)(long)cmd.ExecuteScalar();
                    lstShow.Items.Add(new ShowListAdapter(id, dlg.ShowName, dlg.NameQuery, dlg.DetailsQuery));
                }
            }
        }


        private void editDownloadRequests(object sender, EventArgs e)
        {
            ShowListAdapter sl = (ShowListAdapter)lstShow.SelectedItem;
            if (sl != null)
            {
                EditDownloadRequests dlg = new EditDownloadRequests();
                dlg.ShowId = sl.id;
                dlg.ShowDialog();
            }
        }

        private void editDownloadedEpisodes(object sender, EventArgs e)
        {
            ShowListAdapter sl = (ShowListAdapter)lstShow.SelectedItem;
            if (sl != null)
            {
                EditDownloadedEpisodes dlg = new EditDownloadedEpisodes();
                dlg.ShowId = sl.id;
                dlg.ShowDialog();
            }
        }

        private void feedsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditFeeds dlg = new EditFeeds();
            dlg.ShowDialog();
        }

        private void lstShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstShow.SelectedItem != null)
            {
                lstShow.ContextMenuStrip = contextMenuStrip1;
            }
            else
            {
                lstShow.ContextMenuStrip = null;
            }
        }
    }
}
